package com.blogs.ui;


import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blogs.component.BaseActivity;
import com.blogs.entity.UserSid;
import com.blogs.service.UserLoginTask;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.GetAuthCodePicNew;
import com.cnblogs.R;

public class UserLoginActivity extends BaseActivity implements OnClickListener{
	private class LoginSate
	{
		public static final int succeed = 0;//登录成功
        public static final int server_error = 1;//博客园服务器错误,登录失败
        public static final int input_error = 2;//用户名,密码,验证码错误,登录失败
        public static final int authcode_error = 3;//要输入验证码登录,登录失败  
	}
	private DBSharedPreferences sp;
	private Toast toast; 
	private String aid="";
	private String authcode="";
	private LayoutInflater mInflater;
	private View authcode_dialog_view;
	private ImageView authcode_iv;
	private EditText authcode_et;
	private Button authcode_submit;
	private Button authcode_cancel;
	private AlertDialog authDialog;
	private ProgressDialog dialog;
	private Button title_include_back;
	private TextView title_include_title;
	private RelativeLayout title_top_bg;
	
	private LinearLayout userlogin_body;
	private LinearLayout authcode_show_ll;
	private LinearLayout authcode_loading_ll;
	
	private EditText username_et;
	private EditText userpwd_et;
	private Button login_submit_btn;
	
	private String from;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userlogin);
		sp= new DBSharedPreferences(this);
		ViewGroup headview=(ViewGroup)findViewById(R.id.main_title_include);
		title_top_bg=(RelativeLayout)headview;
		title_include_title=(TextView)headview.findViewById(R.id.title_include_title);
		title_include_back=(Button)headview.findViewById(R.id.title_include_back);
		title_include_title.setText("登录到博客园");
		title_include_back.setVisibility(View.VISIBLE);
		title_include_back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		userlogin_body=(LinearLayout)findViewById(R.id.userlogin_body);
		username_et=(EditText)findViewById(R.id.username_et);
		userpwd_et=(EditText)findViewById(R.id.userpwd_et);
		login_submit_btn=(Button)findViewById(R.id.login_submit_btn);
		login_submit_btn.setOnClickListener(this);
		
		from=getIntent().getStringExtra("from");
		
		this.InitSkin();
		this.initDialog();
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			title_top_bg.getBackground().setLevel(2);
			userlogin_body.getBackground().setLevel(2);
		}
		else
		{
			title_top_bg.getBackground().setLevel(1);
			userlogin_body.getBackground().setLevel(1);
			
		}
	}
	private void initDialog()
	{
		//初始化对话框
		dialog = new ProgressDialog(this);
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		dialog.setMessage("正在登录...");
		
		
		mInflater = getLayoutInflater();
		authcode_dialog_view = mInflater.inflate(R.layout.authcode_dialog_view, null);
		authcode_iv=(ImageView)authcode_dialog_view.findViewById(R.id.authcode_iv);
		authcode_et=(EditText)authcode_dialog_view.findViewById(R.id.authcode_et);
		authcode_submit=(Button)authcode_dialog_view.findViewById(R.id.authcode_submit);
		authcode_cancel=(Button)authcode_dialog_view.findViewById(R.id.authcode_cancel);
		authcode_show_ll=(LinearLayout)authcode_dialog_view.findViewById(R.id.authcode_show_ll);
		authcode_loading_ll=(LinearLayout)authcode_dialog_view.findViewById(R.id.authcode_loading_ll);
		authcode_submit.setOnClickListener(this);
		authcode_cancel.setOnClickListener(this);
		authDialog = new AlertDialog.Builder(UserLoginActivity.this).setView(authcode_dialog_view).create();
		
		
	}
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==login_submit_btn)
		{
			if(username_et.getText().toString().trim().equals(""))
			{
				showToast("用户名不能为空");
				return;
			}
			if(userpwd_et.getText().toString().trim().equals(""))
			{
				showToast("密码不能为空 ");
				return;
			}
			this.toLogin();
			
		}
		else if(v==authcode_submit)
		{
			this.authcode=authcode_et.getText().toString().trim();
			if(this.authcode.equals(""))
			{
				showToast("请输入验证码");
				return;
			}
			this.toLogin();
			authDialog.hide();
		}
		else if(v==authcode_cancel)
		{
			authDialog.hide();
		}
	}
	private void toLogin()
	{
		dialog.show();
		new UserLoginTask(username_et.getText().toString().trim(), userpwd_et.getText().toString().trim(),this.aid,this.authcode,
				new UserLoginTask.IMsgCallBack() {

					public void onSuccess(UserSid user) {
						// TODO Auto-generated method stub
						dialog.hide();
						aid="";
						authcode="";
						switch (Integer.parseInt(user.state)) {
						case LoginSate.succeed:
							sp.SetSessionID(user.sid);
							sp.SetUserName(user.username);
							sp.SetNickName(user.nickname);
							sp.SetUserID(user.uid);
							sp.SetUserAvatar(user.avatar);
							showToast(user.nickname + "登录成功");
							if(from!=null&&!from.equals(""))
							{
								Intent intent=new Intent(from);
								startActivity(intent);
							}
							finish();
							break;
						case LoginSate.input_error:
							showToast(user.error_text);
							break;
						case LoginSate.authcode_error:
							aid=user.aid;
							authDialog.show();
							isShowCode(false);//加载验证码中
							authcode_et.setText("");
							GetAuthCodePicNew.onImageCallBack callback=new GetAuthCodePicNew.onImageCallBack() {
								
								public void onSuccess(Bitmap map) {
									// TODO Auto-generated method stub
									if(map!=null)
									{
										authcode_iv.setImageBitmap(map);
									}
									isShowCode(true);//已经加载验证码
								}
								
								public void onFailed() {
									// TODO Auto-generated method stub
									showToast("获取验证码失败");
									dialog.hide();
								}
							};
							GetAuthCodePicNew get=new GetAuthCodePicNew(callback);
							get.execute(user.aid);
//							showToast("输入验证码");
							break;
						case LoginSate.server_error:
							showToast(user.error_text);
							break;
						}
						
						
					}
					public void onFailed() {
						// TODO Auto-generated method stub
						dialog.hide();
						showToast("网络异常,登录失败");
					}
				}).loadData();
	}
	private void showToast(String text)
	{
		if (toast == null) {
			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
		} else {
			toast.setText(text);
			toast.setDuration(Toast.LENGTH_SHORT);
		}
		toast.show();
	}
	private void isShowCode(boolean show)
	{
		if(show)
		{
			authcode_show_ll.setVisibility(View.VISIBLE);
			authcode_loading_ll.setVisibility(View.GONE);
		}
		else
		{
			authcode_show_ll.setVisibility(View.GONE);
			authcode_loading_ll.setVisibility(View.VISIBLE);
		}
	}
}
